﻿using CommunityToolkit.Mvvm.ComponentModel;
using XYAC.Framework.MonitorTireMaterialOnline.Extensions;

namespace XYAC.Framework.MonitorTireMaterialOnline.ViewModels
{
    public class ViewModelBase : ObservableObject
    {
        /// <summary>
        /// Crc计算器
        /// </summary>
        protected CrcCalculator CrcCalculator = new CrcCalculator();
        protected Action<T> Debounce<T>(Action<T> func, int milliseconds = 300)
        {
            var last = 0;
            return arg =>
            {
                var current = Interlocked.Increment(ref last);
                Task.Delay(milliseconds).ContinueWith(task =>
                {
                    if (current == last) func(arg);
                    task.Dispose();
                });
            };
        }
    }
}
